/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.cms.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.cms.entity.Comment;
import cn.net.ecode.modules.cms.service.CommentService;
import cn.net.ecode.modules.sys.utils.DictUtils;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 评论Controller
 * @author ThinkGem
 * @version 2014-9-16
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/comment")
public class CommentController extends BaseController {

	@Autowired
	private CommentService commentService;
	
	@ModelAttribute
	public Comment get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return commentService.get(id);
		}else{
			return new Comment();
		}
	}
	
	@RequiresPermissions("cms:comment:view")
	@RequestMapping(value = {"list", ""})
	public String list(Comment comment, HttpServletRequest request, HttpServletResponse response, Model model) {
		comment.setStatus(Comment.STATUS_AUDIT);
		return "modules/cms/commentList";
	}
	/**
	 * 查询列表
	 * @param message
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("cms:comment:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<Comment>  listData(Comment comment, HttpServletRequest request, HttpServletResponse response, Model model) {
		return commentService.findPage(new Page<Comment>(request, response), comment);
	}
	

	/** 
	 *  评论保存方法
	 * @author：jiangwenchao
	 * @param comment
	 * @return String  
	 */
	@RequiresPermissions("cms:comment:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(Comment comment) {
		StringBuilder resultMessage = new StringBuilder();
		if (!beanValidator(resultMessage, comment)){
			return renderResult(Global.FALSE, resultMessage);
		}
			if (comment.getAuditUser() == null){
				comment.setAuditUser(UserUtils.getUser());
				comment.setAuditDate(new Date());
			}
			comment.setStatus(Comment.STATUS_NORMAL);
			commentService.save(comment);
			String msg= DictUtils.getDictLabel(comment.getStatus(), "cms_del_flag", "保存")
					+"评论成功";
			return renderResult(Global.TRUE,msg);
	}
	
	/** 
	 *  恢复审核、删除评论
	 * @author：jiangwenchao
	 * @param comment
	 * @param isRe
	 * @param redirectAttributes
	 * @return String  
	 */
	@RequiresPermissions("cms:comment:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(Comment comment, @RequestParam(required=false) Boolean isRe) {
		commentService.delete(comment, isRe);
		return renderResult(Global.TRUE, (isRe!=null&&isRe?"恢复审核":"删除")+"评论成功");
	}

}
